using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace GalaxyDefenderChaos
{
    class CylinderSkyBox
    {
        public Model Model;
        public Texture2D Texture;

        static readonly SamplerState WrapUClampV = new SamplerState
        {
            AddressU = TextureAddressMode.Wrap,
            AddressV = TextureAddressMode.Clamp,
        };

        public void Draw(GraphicsDevice graphicsDevice, Camera camera, GameTime gameTime)
        {
            graphicsDevice.Clear(Color.Black);

            //// Calculate the projection matrix.
            //Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
            //                                                        graphicsDevice.Viewport.AspectRatio,
            //                                                        1, 10000);

            //// Calculate a view matrix, moving the camera around a circle.
            //float time = (float)gameTime.TotalGameTime.TotalSeconds * 0.333f;

            //float cameraX = (float)Math.Cos(time);
            //float cameraY = (float)Math.Sin(time);
            //float cameraZ = (float)Math.Cos(time);

            //Vector3 cameraPosition = new Vector3(cameraX, cameraZ, cameraY) * 64;
            //Vector3 cameraFront = new Vector3(-cameraY, cameraZ, cameraX);

            //Matrix view = Matrix.CreateLookAt(cameraPosition,
            //                                  cameraPosition + cameraFront,
            //                                  Vector3.Up);

            //GraphicsDevice device = Texture.GraphicsDevice;

            // Set renderstates for drawing the sky. For maximum efficiency, we draw the sky
            // after everything else, with depth mode set to read only. This allows the GPU to
            // entirely skip drawing sky in the areas that are covered by other solid objects.
            graphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
            graphicsDevice.SamplerStates[0] = WrapUClampV;
            graphicsDevice.BlendState = BlendState.Opaque;

            // Because the sky is infinitely far away, it should not move sideways as the camera
            // moves around the world, so we force the view matrix translation to zero. This
            // way the sky only takes the camera rotation into account, ignoring its position.
            Matrix view = camera.View;
            view.Translation = Vector3.Zero;

            // The sky should be drawn behind everything else, at the far clip plane.
            // We achieve this by tweaking the projection matrix to force z=w.
            Matrix projection = camera.Projection;
            projection.M13 = projection.M14;
            projection.M23 = projection.M24;
            projection.M33 = projection.M34;
            projection.M43 = projection.M44;

            // Draw the sky model.
            foreach (ModelMesh mesh in Model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.View = view;
                    effect.Projection = projection;
                    effect.Texture = Texture;
                    effect.TextureEnabled = true;
                }

                mesh.Draw();
            }

            // Set modified renderstates back to their default values.
            graphicsDevice.DepthStencilState = DepthStencilState.Default;
            graphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
        }

    }
}
